Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

918
Views
1822, "Error al agregar la restricción de clave externa. Falta el índice para la restricción

Tengo 3 tablas: usuarios, fotos, avatares. Un avatar es una foto del usuario_1 que el usuario_2 ve actualmente cuando se desplaza por las fotos del usuario_1. Diferentes usuarios ven diferentes avatares para cada usuario (depende de cuántas fotos hayan desplazado).

Cuando el usuario eliminó todas sus fotos y avatares, lo que se muestra a otros usuarios también elimina ¿Qué hago mal? (user_id que obtengo de la API del servidor, id solo por conveniencia)

 cursor.execute("""CREATE TABLE IF NOT EXISTS users( id int AUTO_INCREMENT PRIMARY KEY, user_id INTEGER NOT NULL UNIQUE, goal VARCHAR(255) DEFAULT NULL, gender VARCHAR(255) DEFAULT NULL, dob DATE DEFAULT NULL, country VARCHAR(255) DEFAULT NULL, city VARCHAR(255) DEFAULT NULL)""") cursor.execute("""CREATE TABLE IF NOT EXISTS photos( id INTEGER AUTO_INCREMENT, user_id INTEGER NOT NULL, photo VARCHAR(255) NOT NULL, KEY (id), PRIMARY KEY (user_id, photo), FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE)""") cursor.execute("""CREATE TABLE IF NOT EXISTS avatars ( id INTEGER AUTO_INCREMENT PRIMARY KEY, user_id INTEGER, avatar VARCHAR(255), shower_id INTEGER, FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE, FOREIGN KEY (avatar) REFERENCES photos (photo) ON DELETE CASCADE, FOREIGN KEY (shower_id) REFERENCES users (user_id) ON DELETE CASCADE)""")

pero me sale un error

 1822, "Failed to add the foreign key constraint. Missing index for constraint 'avatars_ibfk_2' in the referenced table 'photos'")

versión

 mysqlsh.exe --version C:\Program Files\MySQL\MySQL Shell 8.0\bin\mysqlsh.exe Ver 8.0.19 for Win64 on x86_64 - for MySQL 8.0.19 (MySQL Community Server (GPL))
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Intente seguir la consulta en lugar de la tercera consulta de creación.

 ALTER TABLE photos ADD INDEX(photo); CREATE TABLE IF NOT EXISTS avatars ( id INTEGER AUTO_INCREMENT PRIMARY KEY, user_id INTEGER, avatar VARCHAR(255), shower_id INTEGER, FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE, FOREIGN KEY (avatar) REFERENCES photos (photo) ON DELETE CASCADE, FOREIGN KEY (shower_id) REFERENCES users (user_id) ON DELETE CASCADE );

Debe agregar el índice antes de aplicar la clave externa.

over 4 years ago · Santiago Trujillo Report

0

Necesitas un índice en la foto

 cursor.execute("""CREATE TABLE IF NOT EXISTS photos( id INTEGER AUTO_INCREMENT, user_id INTEGER NOT NULL, photo VARCHAR(255) NOT NULL, KEY (id), KEY (photo), PRIMARY KEY (user_id, photo), FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE)""")

Consulte aquí https://dba.stackexchange.com/questions/268890/1822-failed-to-add-the-foreign-key-constraint-missing-index-for-constraint

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!